//
//  TableViewController.m
//  弹出视图测试版
//
//  Created by AgileMobi on 15/12/15.
//  Copyright © 2015年 Agilemobi. All rights reserved.
//

#import "TableViewController.h"

@interface TableViewController ()

@end
static NSString *cellID = @"popCell";

@implementation TableViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"流程图";
    self.rigth = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(test)];
        self.navigationItem.rightBarButtonItem =self.rigth;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView setTag:10000];
    NSLog(@"%@",self.arr);

}
-(NSMutableArray *)arr{
    if (!_arr) {
        _arr = [[NSMutableArray alloc]init];
    
        for (int i=0 ; i<20; i++) {
            PopModel *pop = [[PopModel alloc]init];
            if (i%2==0) {
                pop.title =@"ttttt";
               
            }
            else
            {
            pop.title =@"test";
            }
             pop.selected = NO;
            [_arr addObject:pop];
            NSLog(@"%@",pop.title);
        }
    }
  
    return _arr;
}
//这个是点击加号按钮触发的事件
-(void)test{
    self.PopView = [[UIView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width-10*2, self.tableView.frame.size.height*0.6)];
    self.PopView.backgroundColor = [UIColor yellowColor];
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self.PopView];
//    [self.view addSubview:self.PopView];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-10*2, 40)];
    bgView.backgroundColor = [UIColor colorWithRed:85/255.0f green:85/255.0f blue:85/255.0f alpha:1];
    [self.PopView addSubview:bgView];
    
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(10, 0, 60, 40);
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cancleBtn];
    [self setTag:11111 WithBtn:cancleBtn];
    
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(self.view.frame.size.width-90, 0, 60, 40);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:confirmBtn];
    [self setTag:22222 WithBtn:confirmBtn];
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-140)/2+40, 0, 60, 40)];
    label.text =@"筛选";
    label.textColor = [UIColor whiteColor];
    [bgView addSubview:label];
    
    
    self.popTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, self.PopView.frame.size.width, self.PopView.frame.size.height-40) style:UITableViewStylePlain];
        self.popTable.dataSource =self;
        self.popTable.delegate = self;
    [self.popTable registerClass:[popCell class] forCellReuseIdentifier:cellID];
    
    self.isFunnel =YES;
    [self.PopView addSubview:self.popTable];
    self.rigth.enabled =NO;
    [self.popTable setTag:10001];

}
-(void)setTag:(NSInteger )tag WithBtn:(UIButton *)btn{
    [btn setTag:tag];
}
//取消操作
-(void)cancel{
    self.PopView.hidden =YES;
    self.rigth.enabled =YES;
    for (int i =0; i<self.arr.count; i++) {
        
        PopModel  *model =  self.arr[i];
        
        model.selected =NO;
    }

}
//确定操作
-(void)confirm{
   
    self.rigth.enabled =YES;
    self.PopView.hidden =YES;
    NSLog(@"%ld",self.popCell.tag);

    NSLog(@"self.text%@",self.text);
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//        判断选中的是哪个tableview
    if (tableView.tag==10001) {
//        弹出视图的tableview
        return self.arr.count;
    }
       else{
//        系统的tableview
    return 30;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag==10001) {
         popCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
//        popCell *cell = [[popCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//        popCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
        PopModel  *model =  self.arr[indexPath.row];
        cell.label.text =model.title;
        cell.btn.selected =model.selected;
        if (cell.btn.selected== NO) {
            [cell.btn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
        }
        else{
           [cell.btn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal];
        }

        [cell.btn setTag:indexPath.row];
//        [cell.btn addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchUpInside];
        if (cell == nil) {
            cell = [[popCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        else{
            while ([cell.contentView.subviews lastObject]!=nil) {
                [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
            }
        }
               return cell;
    }
    else
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.text = @"2222";
    cell.imageView.image = [UIImage imageNamed:@"200.jpg"];
         return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    if (tableView.tag == 10001) {
        popCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        PopModel  *model =  self.arr[indexPath.row];
        if (cell.btn.selected== NO) {
            cell.btn.selected = YES;
            NSLog(@"cell.label.text%@",cell.label.text);
            self.text = [NSString stringWithString:cell.label.text];
        }
        else{
            cell.btn.selected = NO;
        }
        model.selected = cell.btn.selected;
//        进行单行选择
        for (int i=0; i<self.arr.count; i++) {
            PopModel *pop = self.arr[i];
            if (![pop isEqual:model]) {
                pop.selected = NO;
            }
        }
        [self.popTable reloadData];
    }
    else
    {
    
    }
    
}
/*
-(void)select:(UIButton *)sender{
    UIButton *btn = (UIButton *)sender;
   
    if (!btn.selected) {
        [btn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
    }
    else{
        [btn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
    }
   btn.selected = !btn.selected;
}
 */

@end
